#!/usr/bin/perl

require "quotamin-lib.pl";
&ReadParse();

my ($dir, $lable, $mode, $n, $mykey, $i, @order, $status, $index, $tablelength, $username);
my ($sortmode, %user, @kmgstr, $trcls, $display, %pdcuser, @pdcuserfile, $listtitle, @sortselect);

$dir = $in{"checkeddir"};
$lable = $in{"checkedlable"};
$mode = $in{"checkedmode"};
$sortmode = $in{"sortmode"};
$display = $in{"i_display"};

unless($lable)
{
	$lable = $in{"vlable"};
}
unless($display)
{
	$display = "localuser";
}

#$listtitle = &text('listuser_title', $lable);

#&header($listtitle, "", "quotaentries", 0, 1);

$dir = &get_volume_dir($lable);

if($dir eq "vol_name_err")
{
	&error_setup($text{"error_title_listuser"});
	&error($text{'error_msg_vol_name_format'});
}

$listtitle = &text('listuser_title', $lable);

&header($listtitle, "", "quotaentries", 0, 1);

if($display eq "localuser")
{
	$n = &replocalquota(\%user, $dir)
	#$n = &repquotauser(\%user, $dir);
}
else
{
	$n = &reppdcquota(\%user, $dir);
}

print "<form name=formuserlist action=listuser.cgi method=post onsubmit='return onsubmit_form();'>";

print "<table class=mode1 border=0 width=100%>";

print "<tr><td width=80% valign=top>";

print "<div style='valign:top;width:100%;height:300;overflow:auto'>";

print "<table class=mode1 border=0 celladding=0 cellspacing=0 width=100%>";

print "<tr class=title1><th width= 35% align=left>$text{'listuser_th_username'}</th><th align=left>$text{'listuser_th_status'}</th><th align=left>$text{'listuser_th_spaceused'}</th><th align=left>$text{'listuser_th_quotalimit'}</th><th align=left>$text{'listuser_th_warnlimit'}</th></tr>";

@order = (0..$n-1);

if($sortmode eq "ublocks")
{
	@order = sort {$user{"$a^ublocks"} <=> $user{"$b^ublocks"}} @order;
}
elsif($sortmode eq "status")
{
	@order = sort { $user{"$a^status"} cmp $user{"$b^status"} } @order;
}
elsif($sortmode eq "sblocks")
{
	@order = sort {$user{"$a^sblocks"} <=> $user{"$b^sblocks"}} @order;
}
elsif($sortmode eq "hblocks")
{
	@order = sort {$user{"$a^hblocks"} <=> $user{"$b^hblocks"}} @order;
}
else
{
	@order = sort { $user{"$a^user"} cmp $user{"$b^user"} } @order;
}

$i = 0; $table_length = 0;

#if($display eq "localuser")
#{

while($i < @order)
{
	$index = $order[$i];

	if(($user{"$index^sblocks"} eq 0) && ($user{"$index^hblocks"} eq 0))
	{
		$status = $text{"status_limit_no"};
	}
	elsif(($user{"$index^hblocks"} <= $user{"$index^ublocks"}) && ($user{"$index^hblocks"} > 0))
	{
		$status = $text{"status_limit_exceed"};
	}
	elsif($user{"$index^sblocks"} <= $user{"$index^ublocks"} && ($user{"$index^sblocks"} > 0))
	{
		$status = $text{"status_limit_warning"};
	}
	else
	{
		$status = $text{"status_limit_ok"};
	}
	if($table_length %2)
	{
		$trcls = "class=mode2";
	}
	else
	{
		$trcls = "class=mode1";
	}
	$table_length++;
	$username = $user{"$index^user"};
	$username =~ s/\\/\\\\/;
	print "<tr $trcls><td><input name=username type=radio onclick=\'document.forms[0].s_username.value=\"$username\"; \' value=\"$username\">";
	#print $user{"$index^user"}, "</td><td>" , "$status", $user{"$index^bgrace"}, "</td><td>";
	print $user{"$index^user"}, "</td><td>" , "$status";

	if($status eq $text{"status_limit_warning"})
	{
		print $user{"$index^bgrace"};
	}
	
	print "</td><td>";

	@kmgstr = &blocks_to_KMG_str($user{"$index^ublocks"});
	if($kmgstr[2] eq "integer")
	{
		printf "%d$kmgstr[1]", $kmgstr[0];
	}
	else
	{
		printf "%5.2f$kmgstr[1]", $kmgstr[0];
	}

	print "</td><td>";
	@kmgstr = &blocks_to_KMG_str($user{"$index^hblocks"});
	if($status eq $text{"status_limit_no"})
	{
		print "$status";
	}
	else
	{

		if($kmgstr[2] eq "integer")
		{
			printf "%d$kmgstr[1]", $kmgstr[0];
		}
		else
		{
			printf "%5.2f$kmgstr[1]", $kmgstr[0];
		}
	}

	print "</td><td>";

	@kmgstr = &blocks_to_KMG_str($user{"$index^sblocks"});
	if($status eq $text{"status_limit_no"})
	{
		print "$status";
	}
	else
	{
		if($kmgstr[2] eq "integer")
		{
			printf "%d$kmgstr[1]", $kmgstr[0];
		}
		else
		{
			printf "%5.2f$kmgstr[1]", $kmgstr[0];
		}
	}

	print "</td></tr>";
	$i++;
}

print "</table>";
print "</div>";
print "</td><td width=20% valign=top align=center>";
print "<table class=mode1>";
print "<tr><td><input class=button name=submit_setquota type=submit onclick=\'return on_setquota_click($table_length, \"$text{'error_msg_select_empty'}\");\' value=\"$text{'listuser_submit_setquota'}\" ></td></tr>";
if($display eq "pdcuser")
{
	print "<tr><td><input class=button name=submit_displayuser type=submit onclick=\'return on_displaylocal_click();\' value=\"$text{'listuser_submit_displaylocaluser'}\" ></td></tr>";
}
elsif($display eq "localuser")
{
	print "<tr><td><input class=button name=submit_displayuser type=submit onclick=\'return on_displaypdc_click();\' value=\"$text{'listuser_submit_displaypdcuser'}\" ></td></tr>";
}
print "<tr><td><select name=sortselect size=1 class=list3 onchange='return onchange_sortselect();'>";
@sortselect = ($text{'sort_name'}, $text{'sort_status'}, $text{'sort_ub'}, $text{'sort_hb'}, $text{'sort_sb'});
if($sortmode eq "name")
{
	$sortselect[0] = $text{'sort_name_selected'};
}
if($sortmode eq "status")
{
	$sortselect[1] = $text{'sort_status_selected'};
}
if($sortmode eq "ublocks")
{
	$sortselect[2] = $text{'sort_ub_selected'};
}
if($sortmode eq "hblocks")
{
	$sortselect[3] = $text{'sort_hb_selected'};
}
if($sortmode eq "sblocks")
{
	$sortselect[4] = $text{'sort_sb_selected'};
}
print @sortselect;
print "</select></td></tr>";
print "</table>";
print "</td></tr>";
print "</table>";

print "<input name=s_username type=hidden value=''>";
print "<input name=sortmode type=hidden value=\"$sortmode\">";
print "<input name=fsdir type=hidden value=\"$dir\">";
print "<input name=checkeddir type=hidden value=\"$dir\">";
print "<input name=vlable type=hidden value=\"$lable\">";

print "<input name=checkedmode type=hidden value=\"$mode\">";
print "<input name=i_display type=hidden value=\"$display\">";

print "</form>";

print <<EOF;
<script>
function onsubmit_form()
{
        document.forms[0].submit_setquota.disabled = true;
        document.forms[0].submit_displayuser.disabled = true;
}

function onchange_sortselect()
{
        document.forms[0].sortmode.value = document.forms[0].sortselect.value;
        document.forms[0].action = "listuser.cgi";
        return document.forms[0].submit();
}

function on_setquota_click(length, error_msg)
{
	var i;
	var find = 0;

	if(length < 2)
	{
		if(length < 1)
		{
			return false;
		}
		else
		{
			if(document.forms[0].username.checked)
			{
				find = 1;
			}
		}
	}
	else
	{
		for(i = 0; i < length; i++)
		{
			if(document.forms[0].username[i].checked)
			{
				find = 1;
			}
		}
	}

	if(find == 0)
	{
		//alert("please select an item");
		alert(error_msg);
		return false;
	}


	document.forms[0].action = "edit_user_quota.cgi";
}

function on_displaylocal_click()
{
	document.forms[0].i_display.value = "localuser";
	document.forms[0].action = "listuser.cgi";
}

function on_displaypdc_click()
{

	document.forms[0].i_display.value = "pdcuser";
	document.forms[0].action = "listuser.cgi";
}

</script>
EOF

&footer("/quotamin/", $text{'index'});
